package com.lzx.adx.report.support.strategy.channel;

import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson.JSONObject;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.model.common.CommonReportRequest;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import com.lzx.adx.report.util.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
@ProductType(ProductChannelConstant.KK)
public class KuaKeReportStrategy extends AbstractReport{
    @Override
    public String getReportFinalUrl(ReportDTO reportDTO, AccountDTO accountDTO) {

        CommonReportRequest commonReportRequest= JSONObject.parseObject(reportDTO.getExtend(),CommonReportRequest.class);
        Map<String,Object> params=new HashMap<>();
        params.put("adGroupld",reportDTO.getAdgroupId());
        params.put("campaignld",reportDTO.getCampaignId());
        params.put("creativeld",reportDTO.getContentId());

        if(StringUtils.isNotBlank(reportDTO.getOaid()) && !StringUtils.contains(reportDTO.getOaid(), "__")){
            params.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) && !StringUtils.contains(reportDTO.getOaidSum(), "__")){
            params.put("oaidSum", reportDTO.getOaidSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) && !StringUtils.contains(reportDTO.getImeiSum(), "__")){
            params.put("imeiSum", reportDTO.getImeiSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getImei()) && !StringUtils.contains(reportDTO.getImei(), "__")){
            params.put("imei", reportDTO.getImei());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfaSum()) && !StringUtils.contains(reportDTO.getIdfaSum(), "__")){
            params.put("idfaSum", reportDTO.getIdfaSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfa()) && !StringUtils.contains(reportDTO.getIdfa(), "__")){
            params.put("idfa", reportDTO.getIdfa());
        }
        if(StringUtils.isNotBlank(reportDTO.getCaid()) && !StringUtils.contains(reportDTO.getCaid(), "__")){
            params.put("caid", URLUtil.encode(reportDTO.getCaid(), StandardCharsets.UTF_8));
        }


       if(StringUtils.isNotBlank(reportDTO.getUser_agent())){
           //防止媒体编码了，先解码（无编码的解码是不做任何改动的），再编码
           String ua=URLEncoderDecoderTool.decodeURL(reportDTO.getUser_agent());
           params.put("ua", URLEncoderDecoderTool.encodeURL(URLUtil.encode(ua)));
       }
        params.put("ip",reportDTO.getIp());
        params.put("os","android");
        params.put("time",reportDTO.getTraceTime());

        String callbackUrl=String.format(accountDTO.getDspCallbackUrl(),reportDTO.getBizRequestId());

        String urlParams= UrlUtil.covertParamsOrderByAsc(params)+"&callbackUrl="+ URLEncoderDecoderTool.encodeURL(callbackUrl);

        String ul=accountDTO.getDspReportUrl()+"&"+urlParams;
        if(ul.contains("__CSITE__") && StringUtils.isNotBlank(commonReportRequest.getSid())){
            ul = ul.replace("__CSITE__", commonReportRequest.getSid());
        }
        if(ul.contains("__POS__") && StringUtils.isNotBlank(commonReportRequest.getPos())){
            ul = ul.replace("__POS__", commonReportRequest.getPos());
        }
        if(ul.contains("__ENCRYPTED_POSITION_ID__") && StringUtils.isNotBlank(commonReportRequest.getPos())){
            ul = ul.replace("__ENCRYPTED_POSITION_ID__", commonReportRequest.getPos());
        }
        if(ul.contains("__PROMOTED_OBJECT_TYPE__") && StringUtils.isNotBlank(commonReportRequest.getSid())){
            ul = ul.replace("__PROMOTED_OBJECT_TYPE__", commonReportRequest.getSid());
        }
        if(ul.contains("__UNION_SITE__") && StringUtils.isNotBlank(commonReportRequest.getPos())){
            ul = ul.replace("__UNION_SITE__", commonReportRequest.getPos());
        }

        return ul;
    }
}
